﻿#include <stdio.h>
//base64编码表
char base64_alphabet[]= {
	'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P', 
	'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f', 
	'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v', 
	'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/','='}; 

/**********************************************************
 *函数名：Base64Decode
 *功能：对base64编码进行解码
 *入口参数：base64code:base64编码序列指针 base64length:序列长度
 *出口参数：解码后的字节序列指针
 *说明：用户调用此函数以后必须将返回指针所对内存释放,用delete
 *	    方法,base64length参数必须为4的倍数
**********************************************************/

char* Base64Decode(char *base64code, unsigned int base64length){ 

	char temp[4];
	char *chtemp=temp;
	char *buffertemp;
	char *buffer;
	char num=0;

	buffer=new char[base64length/4*3+1];
	buffertemp=buffer;
	for(unsigned int i=0; i<base64length/4;i++){

		chtemp=temp;
		for(unsigned int m=0;m<4;m++){
			
			if(((*base64code)>='a')&&((*base64code)<='z')){
				*chtemp=(*base64code)-'a'+26;
			}else if(((*base64code)>='A')&&((*base64code)<='Z')){
				*chtemp=(*base64code)-'A';
			}else if(((*base64code)>='0')&&((*base64code)<='9')){
				*chtemp=(*base64code)-'0'+52;
			}else if(*base64code=='/'){
				*chtemp=63;
			}else if(*base64code=='+'){
				*chtemp=62;
			}else{
				*chtemp='=';
			}
			chtemp++;
			base64code++;
		}
		*buffertemp++=(temp[0]<<2)+((temp[1]>>4));
		*buffertemp++=(temp[1]<<4)+((temp[2]>>2));
		*buffertemp++=(temp[2]<<6)+((temp[3]));			
	}
	if(temp[2]=='=')
		num++;
	if(temp[3]=='=')
		num++;

	*(buffertemp-num)=0;
	return buffer;

} 

/**********************************************************
 *函数名：Base64Encode
 *功能：对给定字节序列进行base64编码
 *入口参数：base64code:要编码的字节序列指针 base64length:要编码
 *		    字节序列的长度
 *出口参数：编码后的字节序列指针
 *说明：用户调用此函数以后必须将返回指针所对内存释放,用delete
 *	    方法
**********************************************************/

char* Base64Encode(char *base64code, unsigned int base64length){

	char *buffer;
	char *temp;
	char base64_alphabet[65];
	unsigned int i;

	buffer=new char[base64length%3?(base64length/3*4+1):(base64length/3*4)+1];
	temp=buffer;

	//动态生成编码表
	for(i=0;i<65;i++){
	
		if(i<26){
		
			base64_alphabet[i]=i+'A';

		}else if((i>=26)&&(i<52)){

			base64_alphabet[i]=i-26+'a';
		
		}else if((i>=52)&&(i<62)){

			base64_alphabet[i]=i-52+'0';
		
		}else if(i==62){
		
			base64_alphabet[i]='+';
		
		}else if(i==63){
		
			base64_alphabet[i]='/';
	
		}else{
		
			base64_alphabet[i]='=';

		}

	}
	for(i=0; i<base64length/3;i++){
	
		*temp++=base64_alphabet[((*base64code)>>2)&0x3f];
		*temp++=base64_alphabet[(((*base64code)<<4)&0x3F)+(((*(base64code+1))>>4)&0x0f)];
		*temp++=base64_alphabet[(((*(base64code+1))<<2)&0x3F)+(((*(base64code+2))>>6)&0x03)];
		*temp++=base64_alphabet[*(base64code+2)&0x3F];
		base64code+=3;

	}
	if((base64length%3)==1){
		*(temp-1)='=';
		*(temp-2)='=';
	}
	if((base64length%3)==2){
		*(temp-1)='=';
	}
	*temp=0;
	return buffer;
}
